package com.zhx.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhx.config.Constant;
import com.zhx.dao.MenuDao;
import com.zhx.menu.Menu;
import com.zhx.vo.Page;
/**
 * 权限树
 * 
 * @author 演示
 * 
 */
public class MenuServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m = req.getParameter("m");
		if ("list".equals(m)) {
			process(req, resp, "/page/menu/menu_list.jsp");
		} else if ("openAdd".equals(m)) {
			process(req, resp, "/page/menu/menu_add.jsp");
		} else if ("add".equals(m)) {
			add(req, resp);
		} else if ("edit".equals(m)) {
			edit(req, resp);
		} else if ("del".equals(m)) {
			del(req, resp);
		} else if ("openEdit".equals(m)) {
			process(req, resp, "/page/menu/menu_edit.jsp");
		}
	}
	
		
	/**
	 * 添加子一级菜单
	 * 
	 * @param request
	 * @param response
	 * @param path
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			// 获取用户输入的参数
			Menu menu = new Menu();
			menu.setMenuId(request.getParameter("menuId"));
			menu.setMenuFatherId(request.getParameter("menuFatherId"));
			menu.setMenuName(request.getParameter("menuName"));
			menu.setUrl(request.getParameter("url"));
			menu.setCreateTime(new Timestamp(System.currentTimeMillis()));
			menu.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			
			// 添加菜单到数据库
			MenuDao menuDao = new MenuDao();
			menuDao.add(menu);
			process(request, response, "/page/menu/menu_list.jsp");
		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	protected void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			// 获取用户输入的参数
			Menu menu = new Menu();
			String id = request.getParameter("id");
			if(id != null) {
				menu.setId(Integer.valueOf(id));
			} 
			menu.setMenuId(request.getParameter("menuId"));
			menu.setMenuFatherId(request.getParameter("menuFatherId"));
			menu.setMenuName(request.getParameter("menuName"));
			menu.setUrl(request.getParameter("url"));
			menu.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			
			// 修改信息
			MenuDao menuDao = new MenuDao();
			menuDao.updateMenu(menu);

			process(request, response, "/page/menu/menu_list.jsp");
		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	protected void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {

			String id = request.getParameter("id");
			// 实例化Menu
			Menu menu = new Menu();
			if(id != null) {
				menu.setId(Integer.valueOf(id));
				// 删除信息
				MenuDao menuDao = new MenuDao();
				menuDao.deleteMenu(menu);
			} 
			
			process(request, response, "/page/menu/menu_list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
    
}
